package com.easyOrder.web.action2.user;

import com.easyOrder.core.comm.BaseAction;
import com.easyOrder.core.context.ApiMethodAttribute;
import com.easyOrder.param.user.LoginParameter;
import com.easyOrder.param.user.LoginResult;
import com.easyOrder.service.common.LoginService;
import com.easyOrder.service.impl.RongcloudService;
import com.easyOrder.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by liutao on 2017/7/15.
 */
@RestController
@RequestMapping("/common/user")
public class UserAction extends BaseAction{

    @Autowired
    private LoginService loginService;

    @Autowired
    private RongcloudService rongCloudService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.OPTIONS})
    @ApiMethodAttribute(nonSessionValidation = true)
    public Object employeeLogin(HttpServletRequest request, @RequestBody LoginParameter loginParameter) {
        LoginResult loginResult = loginService.loginByUser(loginParameter);
        loginResult.setRongClountToken(
                rongCloudService.getToken(
                        loginResult.getUserId().toString(),
                        loginResult.getUserName(),
                        loginResult.getIconPath()));
        String verCode = request.getHeader("verCode");
        userService.logDeviceIdAndVerCode(loginParameter.getDeviceId(), loginParameter.getVendorDeviceId(), verCode, loginResult);
        return wrapperJsonView(loginResult);
    }
}
